
#
# quick and dirty note builder based on a schema
# 
# not dissimilar from active-record scaffolding except different primitives
# 
# useful to do on server side for a variety of reasons such as verifying data
#

require 'rubygems'
require 'json'

def to_html(note,schema,form)

    # load schema
    schema = JSON.parse(File.open(schema))

    # paint form boundaries
    if form
      write "<h3>Please edit post and select 'submit'</h3>\n"
      write "<form name='schema' id='schema' ENCTYPE='multipart/form-data' METHOD='post' action='/note/post'>\n";
    end

    # paint subject matter
    write "<table>"
    schema.each do |n|

      # get field properties
      typ = n['type']
      sub = n['id']
      fill = n['value']

      # use instance properties if supplied
      if note
        fill = note.send sub
      end

      # paint each field
      case type
      when 'icon', 'string', 'region', 'location', 'date', 'show'
        write "<tr>\n<td valign=top><span title=\"#{hint}\" class=popup ><b>#{label}</b></span></td>\n"
      end

      if form
        case typ
        when 'icon'
        when 'string'
          write "<td><input name=\"#{sub}\" value=\"#{fill}\" size=73 /></td>"
        when 'hidden'
        when 'region'
          write "<td><textarea name =\"#{sub}\" rows='15' height='15' cols='55' size='56' maxlength='512'>\"#{fill}\"</textarea></td>\n"
        when 'location'
          write "<td><div width=256 height=256 /></td>"
        when 'date'
        when 'show'
        end
      else
        case typ
        when 'icon'
        when 'string'
          write fill
        when 'hidden'
        when 'region'
          write fill
        when 'location'
        when 'date'
        when 'show'
        end
      end

      case typ
      when 'icon', 'string', 'region', 'date', 'location', 'show'
        write "</tr>\n"
      end

    end
    write "</table>"

    if form
      write "</form>\n"
    end

end


=begin
<style>
form.note label { 
	  width: 140px;
	  margin-right: 10px;
	  float: left;
	  text-align: right;
	  padding-top: 0.2em;
	  font-weight: bold;
	  font-size: 1em;
	}
</style>
=end
